package service;

import model.game.ComputerPlayer;
import model.game.Game;
import model.game.HumanPlayer;
import model.game.Player;
import model.game.PointController;
import model.game.Table;

/**
 *
 * @author Dustin
 */
public class GameController {
    public final static int DEFAULT = -1;
    public final static int SWAP = 0;
    public final static int SWAP_ALL = 1;
    public final static int PASS = 2;
    public final static int DEAL = 3;

    private Game g;

    public GameController(Game aGame) {
        g = aGame;
    }

    public Game getGame() {
        return g;
    }

    public Table getTable() {
        return g.getTable();
    }

    public void setGame(String name) {
        g = new Game(name);
    }

    public void addPlayer(String aName, int seat) {
        Player p = (aName != null ? new HumanPlayer(aName) : null);
        g.setPlayer(seat, p);
    }

    public void doTurn(int turnType, int handIndex, int tableIndex) {
        Player currentPlayer = g.getCurrentPlayer();
        switch(turnType) {
            case SWAP:
                currentPlayer.swapCard(g.getTable(), handIndex, tableIndex);
                break;
            case SWAP_ALL:
                currentPlayer.swapCards(g.getTable());
                break;
            case PASS:
                currentPlayer.pass();
                break;
            case DEAL:
                currentPlayer.deal(g.getTable(), g.getPlayers());
                break;
            default:
                break;
        }
        if(PointController.determinePoints(currentPlayer.getHand()) != PointController.BLITZ) {
            g.setNextPlayer();
            currentPlayer = g.getCurrentPlayer();

            if(currentPlayer instanceof ComputerPlayer) {
                ((ComputerPlayer) currentPlayer).doTurn(g.getTable());
                //check blitz pass
                doTurn(DEFAULT,-1,-1);
            }
        } else {
            //blitz pass
        }
    }
}
